﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using iWare.Wms.Core;
using iWare.Wms.Core.Enum;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace iWare.Wms.Application.Service.WmsBase.WmsSeed
{
    /// <summary>
    /// 获取种子数据
    /// </summary>
    [ApiDescriptionSettings("种子数据", Name = "WmsSeed", Order = 100)]
    [Route("api/[Controller]")]
    public class WmsSeed : IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsTask, MasterDbContextLocator> _wmsTask;
        private readonly IRepository<WmsMaterialStock, MasterDbContextLocator> _wmsMaterialStock;
        private readonly IRepository<WmsContainerPlace, MasterDbContextLocator> _wmsContainerPlace;
        private readonly IRepository<WmsMaterialContainer, MasterDbContextLocator> _wmsMaterialContainer;
        private readonly IRepository<SysDictData, MasterDbContextLocator> _sysDictData;
        private readonly IRepository<SysMenu, MasterDbContextLocator> _sysMenu;

        public WmsSeed(
            IRepository<WmsTask, MasterDbContextLocator> wmsTask,
            IRepository<WmsMaterialStock, MasterDbContextLocator> wmsMaterialStock,
            IRepository<WmsContainerPlace, MasterDbContextLocator> wmsContainerPlace,
            IRepository<WmsMaterialContainer, MasterDbContextLocator> wmsMaterialContainer,
            IRepository<SysDictData, MasterDbContextLocator> sysDictData,
            IRepository<SysMenu, MasterDbContextLocator> sysMenu
        )
        {
            _wmsTask = wmsTask;
            _wmsMaterialStock = wmsMaterialStock;
            _wmsContainerPlace = wmsContainerPlace;
            _wmsMaterialContainer = wmsMaterialContainer;
            _sysDictData = sysDictData;
            _sysMenu = sysMenu;
        }

        /// <summary>
        /// 任务管理实体种子
        /// </summary>
        /// <returns></returns>
        [HttpGet("WmsTaskSeed")]
        [AllowAnonymous]
        public async Task<List<string>> WmsTaskSeed()
        {
            var taskList = await _wmsTask.Where(n => n.IsDeleted == false).ToListAsync();
            var newtaskList = new List<string>();
            var TaskModel1 = "";
            var TaskType1 = "";
            var TaskStatus1 = "";
            foreach (var item in taskList)
            {
                if (item.TaskModel == TaskModel.SHOUDONG) TaskModel1 = "TaskModel.SHOUDONG";
                if (item.TaskModel == TaskModel.ZIDONG) TaskModel1 = "TaskModel.ZIDONG";
                if (item.TaskType == TaskType.RUKU) TaskType1 = "TaskType.RUKU";
                if (item.TaskType == TaskType.CHUKU) TaskType1 = "TaskType.CHUKU";
                if (item.TaskType == TaskType.YIKU) TaskType1 = "TaskType.YIKU";
                if (item.TaskStatus == Core.Enum.TaskStatus.WEIZHIXING) TaskStatus1 = "Core.Enum.TaskStatus.WEIZHIXING";
                if (item.TaskStatus == Core.Enum.TaskStatus.ZHIXINGZHONG) TaskStatus1 = "Core.Enum.TaskStatus.ZHIXINGZHONG";
                if (item.TaskStatus == Core.Enum.TaskStatus.WANCHENG) TaskStatus1 = "Core.Enum.TaskStatus.WANCHENG";
                var task = string.Format("Id={0},TaskNo=\"{1}\",TaskModel={2},TaskType={3},TaskLevel={4},TaskStatus={5},ContainerCode=\"{6}\",SourcePlace=\"{7}\",ToPlace=\"{8}\",Aisle={9},AreaName=\"{10}\",OrderNo=\"{11}\",SendTimes={12}", item.Id, item.TaskNo, TaskModel1, TaskType1, item.TaskLevel, TaskStatus1, item.ContainerCode, item.SourcePlace, item.ToPlace, item.Aisle, item.AreaName, item.OrderNo, item.SendTimes);
                newtaskList.Add("new WmsTask{"+task+ "}");
            }
            return newtaskList;
        }

        /// <summary>
        /// 库存表实体种子
        /// </summary>
        /// <returns></returns>
        [HttpGet("WmsMaterialStockSeed")]
        [AllowAnonymous]
        public async Task<List<string>> WmsMaterialStockSeed()
        {
            var stockList = await _wmsMaterialStock.Where(n => n.IsDeleted == false).ToListAsync();
            var newstockList = new List<string>();
            var MaterialType1 = "";
            var MaterialInspection1 = "";
            var UnitType1 = "";
            var UnitnoType1 = "";
            foreach (var item in stockList)
            {
                if (item.MaterialType == MaterialType.YUANCAILIAO) MaterialType1 = "MaterialType.YUANCAILIAO";
                if (item.MaterialType == MaterialType.BANCHENGPING) MaterialType1 = "MaterialType.BANCHENGPING";
                if (item.MaterialType == MaterialType.CHENGPING) MaterialType1 = "MaterialType.CHENGPING";
                if (item.MaterialType == MaterialType.KONGTUO) MaterialType1 = "MaterialType.KONGTUO";
                if (item.InspectionMethod == MaterialInspection.MIANJIAN) MaterialInspection1 = "MaterialInspection.MIANJIAN";
                if (item.InspectionMethod == MaterialInspection.CHOUJIAN) MaterialInspection1 = "MaterialInspection.CHOUJIAN";
                if (item.UnitType == UnitType.CHANGDU) UnitType1 = "UnitType.CHANGDU";
                if (item.UnitType == UnitType.ZHONGLIANG) UnitType1 = "UnitType.ZHONGLIANG";
                if (item.UnitNo == UnitnoType.CM) UnitnoType1 = "UnitnoType.CM";
                if (item.UnitNo == UnitnoType.T) UnitnoType1 = "UnitnoType.T";
                var stock = string.Format("Id={0},MaterialNo=\"{1}\",MaterialType={2},MaterialBatch=\"{3}\",MaterialName=\"{4}\",MaterialSpec=\"{5}\",InspectionMethod={6},UnitType={7},UnitNo={8},StockNumber=(decimal){9},PlaceCode=\"{10}\",ContainerCode=\"{11}\",AreaId={12}", item.Id, item.MaterialNo, MaterialType1, item.MaterialBatch, item.MaterialName, item.MaterialSpec, MaterialInspection1, UnitType1, UnitnoType1, item.StockNumber, item.PlaceCode, item.ContainerCode, item.AreaId);
                newstockList.Add("new WmsMaterialStock{" + stock+ "}");
            }
            return newstockList;
        }

        /// <summary>
        /// 托盘库位关系表实体种子
        /// </summary>
        /// <returns></returns>
        [HttpGet("WmsContainerPlaceSeed")]
        [AllowAnonymous]
        public async Task<List<string>> WmsContainerPlaceSeed()
        {
            var placeList = await _wmsContainerPlace.Where(n => n.IsDeleted == false).ToListAsync();
            var newplaceList = new List<string>();
            var CommonStatus1 = "";
            foreach (var item in placeList)
            {
                if (item.ContainerPlaceStatus == CommonStatus.ENABLE) CommonStatus1 = "CommonStatus.ENABLE";
                if (item.ContainerPlaceStatus == CommonStatus.DISABLE) CommonStatus1 = "CommonStatus.DISABLE";
                if (item.ContainerPlaceStatus == CommonStatus.DELETED) CommonStatus1 = "CommonStatus.DELETED";
                var place = string.Format("Id={0},PlaceId={1},PlaceCode=\"{2}\",ContainerId={3},ContainerCode=\"{4}\",ContainerPlaceStatus={5}", item.Id, item.PlaceId, item.PlaceCode, item.ContainerId, item.ContainerCode, CommonStatus1);
                newplaceList.Add("new WmsContainerPlace{"+place+"}");
            }
            return newplaceList;
        }

        /// <summary>
        /// 物料托盘关系表实体种子
        /// </summary>
        /// <returns></returns>
        [HttpGet("WmsMaterialContainerSeed")]
        [AllowAnonymous]
        public async Task<List<string>> WmsMaterialContainerSeed()
        {
            var masterList = await _wmsMaterialContainer.Where(n => n.IsDeleted == false).ToListAsync();
            var newmasterList = new List<string>();
            var CommonStatus1 = "";
            foreach (var item in masterList)
            {
                if (item.BindStatus == CommonStatus.ENABLE) CommonStatus1 = "CommonStatus.ENABLE";
                if (item.BindStatus == CommonStatus.DISABLE) CommonStatus1 = "CommonStatus.DISABLE";
                if (item.BindStatus == CommonStatus.DELETED) CommonStatus1 = "CommonStatus.DELETED";
                var master = string.Format("Id={0},ContainerId={1},ContainerCode=\"{2}\",MaterialName=\"{3}\",MaterialNo=\"{4}\",MaterialBatch=\"{5}\",MaterialSpec=\"{6}\",MaterialId={7},BindQuantity=(decimal){8},BindStatus={9},SampleQty=(decimal){10},OrderNo=\"{11}\"", item.Id, item.ContainerId, item.ContainerCode, item.MaterialName, item.MaterialNo, item.MaterialBatch, item.MaterialSpec, item.MaterialId, item.BindQuantity, CommonStatus1, item.SampleQty, item.OrderNo);
                newmasterList.Add("new WmsMaterialContainer{"+master+"}");
            }
            return newmasterList;
        }

        /// <summary>
        /// 字典表实体种子
        /// </summary>
        /// <returns></returns>
        [HttpGet("SysDictDataSeed")]
        [AllowAnonymous]
        public async Task<List<string>> SysDictDataSeed()
        {
            var dictdataList = await _sysDictData.Where(n => n.IsDeleted == false).ToListAsync();
            var newdictdataList = new List<string>();
            foreach (var item in dictdataList)
            {
                var Zhi = string.Format("Id={0},TypeId={1},Value=\"{2}\",Code=\"{3}\",Sort={4},Remark=\"{5}\",Status={6}", item.Id, item.TypeId, item.Value, item.Code, item.Sort, item.Remark, item.Status);
                newdictdataList.Add("new SysDictData{" + Zhi + "}");
            }
            return newdictdataList;
        }

        /// <summary>
        /// 系统菜单表实体种子
        /// </summary>
        /// <returns></returns>
        [HttpGet("SysMenuSeed")]
        [AllowAnonymous]
        public async Task<List<string>> SysMenuSeed()
        {
            var menuList = await _sysMenu.Where(n => n.IsDeleted == false).ToListAsync();
            var newmenuList = new List<string>();
            var MenuType1 = "";
            var MenuOpenType1 = "";
            var MenuWeight1 = "";
            var CommonStatus1 = "";
            foreach (var item in menuList)
            {
                if (item.Type == MenuType.DIR) MenuType1 = "MenuType.DIR";
                if (item.Type == MenuType.MENU) MenuType1 = "MenuType.MENU";
                if (item.Type == MenuType.BTN) MenuType1 = "MenuType.BTN";
                if (item.OpenType == MenuOpenType.NONE) MenuOpenType1 = "MenuOpenType.NONE";
                if (item.OpenType == MenuOpenType.COMPONENT) MenuOpenType1 = "MenuOpenType.COMPONENT";
                if (item.OpenType == MenuOpenType.INNER) MenuOpenType1 = "MenuOpenType.INNER";
                if (item.OpenType == MenuOpenType.OUTER) MenuOpenType1 = "MenuOpenType.OUTER";
                if (item.Weight == MenuWeight.SUPER_ADMIN_WEIGHT) MenuWeight1 = "MenuWeight.SUPER_ADMIN_WEIGHT";
                if (item.Weight == MenuWeight.DEFAULT_WEIGHT) MenuWeight1 = "MenuWeight.DEFAULT_WEIGHT";
                if (item.Status == CommonStatus.ENABLE) CommonStatus1 = "CommonStatus.ENABLE";
                if (item.Status == CommonStatus.DISABLE) CommonStatus1 = "CommonStatus.DISABLE";
                if (item.Status == CommonStatus.DELETED) CommonStatus1 = "CommonStatus.DELETED";
                var Zhi = "";
                if (item.Pid == 0)
                {
                    Zhi = string.Format("Id={0},Pid={1},Pids=\"{2}\",Name=\"{3}\",Code=\"{4}\",Type={5},Icon=\"{6}\",Router=\"{7}\",Component=\"{8}\",Application=\"{9}\",OpenType={10},Visible=\"{11}\",Weight={12},Sort={13},Status={14}", item.Id, item.Pid, item.Pids, item.Name, item.Code, MenuType1, item.Icon, item.Router, item.Component, item.Application, MenuOpenType1, item.Visible, MenuWeight1, item.Sort, CommonStatus1);
                }
                else
                {
                    Zhi = string.Format("Id={0},Pid={1},Pids=\"{2}\",Name=\"{3}\",Code=\"{4}\",Type={5},Permission=\"{6}\",Application=\"{7}\",OpenType={8},Visible=\"{9}\",Weight={10},Sort={11},Status={12}", item.Id, item.Pid, item.Pids, item.Name, item.Code, MenuType1, item.Permission, item.Application, MenuOpenType1, item.Visible, MenuWeight1, item.Sort, CommonStatus1);
                }
                newmenuList.Add("new SysMenu{" + Zhi + "}");
            }
            return newmenuList;
        }
    }
}
